<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>部门管理</title>
    <style>
        .action-buttons .btn {
            margin-right: 5px;
        }
        .modal-header {
            background-color: #007bff;
            color: white;
        }
        .table-actions {
            width: 150px;
            text-align: center;
        }
    </style>
</head>
<body>
<jsp:include page="layout/header.jsp"/>
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">部门管理</h5>
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addDeptModal">
                        <i class="fas fa-plus"></i> 新增部门
                    </button>
                </div>
                <div class="card-body">
                    <!-- 搜索表单 -->
                    <form class="form-inline mb-4" action="list.do" method="get">
                        <div class="input-group">
                            <input class="form-control" type="text" name="name" placeholder="搜索部门名称" value="${name}"/>
                            <div class="input-group-append">
                                <button class="btn btn-primary" type="submit">
                                    <i class="fas fa-search"></i> 查询
                                </button>
                                <a class="btn btn-secondary" href="list.do">
                                    <i class="fas fa-times"></i> 清空
                                </a>
                            </div>
                        </div>
                    </form>
                    
                    <!-- 错误提示 -->
                    <c:if test="${not empty error}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            ${error}
                            <button type="button" class="close" data-dismiss="alert">
                                <span>&times;</span>
                            </button>
                        </div>
                    </c:if>
                    
                    <!-- 数据表格 -->
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="thead-light">
                                <tr>
                                    <th>ID</th>
                                    <th>部门名称</th>
                                    <th>创建时间</th>
                                    <th>更新时间</th>
                                    <th class="table-actions">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach var="d" items="${list}">
                                    <tr>
                                        <td>${d.id}</td>
                                        <td>${d.name}</td>
                                        <td>${d.createTime}</td>
                                        <td>${d.updateTime}</td>
                                        <td class="action-buttons">
                                            <button type="button" class="btn btn-sm btn-primary" 
                                                    onclick="editDept(${d.id}, '${d.name}')" 
                                                    data-toggle="modal" data-target="#editDeptModal">
                                                <i class="fas fa-edit"></i> 编辑
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" 
                                                    onclick="deleteDept(${d.id}, '${d.name}')">
                                                <i class="fas fa-trash"></i> 删除
                                            </button>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-3">
                        <a class="btn btn-secondary" href="../pages/main.jsp">
                            <i class="fas fa-arrow-left"></i> 返回首页
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 新增部门弹窗 -->
<div class="modal fade" id="addDeptModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">新增部门</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="add.do" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="addDeptName">部门名称</label>
                        <input type="text" class="form-control" id="addDeptName" name="name" 
                               placeholder="请输入部门名称" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-success">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑部门弹窗 -->
<div class="modal fade" id="editDeptModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">编辑部门</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="update.do" method="post">
                <input type="hidden" id="editDeptId" name="id">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="editDeptName">部门名称</label>
                        <input type="text" class="form-control" id="editDeptName" name="name" 
                               placeholder="请输入部门名称" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script>
    function editDept(id, name) {
        document.getElementById('editDeptId').value = id;
        document.getElementById('editDeptName').value = name;
    }
    
    function deleteDept(id, name) {
        if (confirm('确认删除部门 "' + name + '" 吗？\n注意：如果该部门下有员工，将无法删除。')) {
            window.location.href = 'delete.do?id=' + id;
        }
    }
</script>

<jsp:include page="layout/footer.jsp"/>
</body>
</html>

